﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Komunikator.Core;
using System.Threading;

namespace Komunikator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        LoginWindow loginWin;
        HttpConnector httpConnector;
        Thread receiver;
        ConnectionHolder conHolder;
        DescriptionController descController;
        DescriptionWindow descWindow;

        public MainWindow()
        {
            InitializeComponent();
            descController = new DescriptionController();
            descWindow = new DescriptionWindow(this);
        }


        public delegate void UpdateTextDelegate(string text);
        public void UpdateText(string text)
        {
            this.richTextBox1.AppendText(text);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            logCtrl.LoginButton.Click += new RoutedEventHandler(LoginButton_Click);
        }

        private void ChangeStatusDescr()
        {
            descWindow.Show();
            descWindow.Closed += new EventHandler(descWindow_Closed);
        }

        void descWindow_Closed(object sender, EventArgs e)
        {
            /*if (DescriptionController.descriptionStatus == true)
            {
                conHolder.packetSender.ChangeStatus(
            }*/
        }

        void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            LoginModel model = logCtrl.GetLoginModel();
            HttpConnector connector = new HttpConnector();
            HttpConnector.ggNumber = Int32.Parse(model.number);
            HttpConnector.ggPassword = model.password;
            connector.CreateMessage(Int32.Parse(model.number));

            conHolder = new ConnectionHolder(this);
            conHolder.Initialize();

            receiver = new Thread(new ThreadStart(conHolder.packetReceiver.ReceivePacket));
            receiver.Name = "receiver";
            receiver.Start();

            logCtrl.Visibility = System.Windows.Visibility.Hidden;
            StatusComboBox.Visibility = System.Windows.Visibility.Visible;
            //throw new NotImplementedException();
        }

        void loginWin_Closed(object sender, EventArgs e)
        { // wszystko co dzieje sie po zamknieciu okna logowania - polaczenie z serwerem, sprawdzenie statusu znajomych, zmiana wlasnego statusu
            //httpConnector = new HttpConnector();
            //packetReceiver = new PacketReceiver(this);
            //packetReceiver.Initialize();
            //packetSender = new PacketSender(this);
            //packetSender.Initialize();
            //receiver = new Thread(new ThreadStart(packetReceiver.ReceivePacket));
            //receiver.Start();
            //receiver.Name = "receiver";
            //this.Show();
            
            
        }


        private void StatusComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch(StatusComboBox.SelectedIndex)
            {
                //0,1 - dost;2,3-zw;4,5-niewidoczny;6,7-niedostepny;
                case 0: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_AVAIL); break;
                case 1: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_AVAIL); break;
                case 2: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_BUSY); break;
                case 3: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_BUSY); break;
                case 4: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_INVISIBLE); break;
                case 5: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_INVISIBLE); break;
                case 6: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_NOT_AVAIL); break;
                case 7: conHolder.packetSender.ChangeStatus(Packets.GG_STATUS_NOT_AVAIL); break;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                receiver.Abort();
            }
            catch { }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            
        }  
    }
}
